package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.constants.TransitConstants;
import com.zrwl.poscloud.base.entity.GradeTeacherR;
import com.zrwl.poscloud.base.entity.SysDictData;
import com.zrwl.poscloud.base.entity.Transit;
import com.zrwl.poscloud.base.entity.TransitSend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.*;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DateUtils;
import com.zrwl.poscloud.commons.utils.JsonUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.MessageModelVO;
import com.zrwl.poscloud.xo.vo.UserEmployeeRVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zrwl.poscloud.xo.mapper.TransitSendMapper;
import com.zrwl.poscloud.xo.vo.TransitSendVO;

import javax.annotation.Resource;

/**
 * 出校通知 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-17
 */
@Service
public class TransitSendServiceImpl extends SuperServiceImpl<TransitSendMapper, TransitSend> implements TransitSendService {

    @Autowired
    private TransitSendService transitSendService;
    @Resource
    private TransitSendMapper transitSendMapper;
    @Autowired
    private GradeTeacherRService gradeTeacherRService;
    @Autowired
    private UserEmployeeRService userEmployeeRService;
    @Autowired
    private TransitService transitService;
    @Autowired
    private MessageModelService messageModelService;
    @Autowired
    private SysDictDataService sysDictDataService;
    @Override
    public IPage<TransitSendVO> getPageList(TransitSendVO transitSendVO) {
        Page<TransitSend> page = new Page<>();
        page.setCurrent(transitSendVO.getCurrentPage());
        page.setSize(transitSendVO.getPageSize());
        IPage<TransitSendVO> pageList = transitSendMapper.getPageList(page, transitSendVO);
        List<TransitSendVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }
    @Override
    public List<TransitSend> getAllList(TransitSendVO transitSendVO) {
        QueryWrapper<TransitSend> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<TransitSend> list = transitSendService.list(queryWrapper);
        return list;
}
    @Override
    public String add(TransitSendVO transitSendVO){
        TransitSend entity = new TransitSend();
        BeanUtils.copyProperties(transitSendVO, entity, SysConf.STATUS);
        entity.insert();
        transitSendVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(TransitSendVO transitSendVO) {
        TransitSend entity = transitSendService.getById(transitSendVO.getUid());
        BeanUtils.copyProperties(transitSendVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }
    @Override
    public String delete(TransitSendVO transitSendVO) {
        TransitSend entity = transitSendService.getById(transitSendVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    /**
     * 消息推送
     *
     * @param transitSendVO 消息推送
     * @return String
     */
    @Override
    public String messagePush(TransitSendVO transitSendVO) {
        TransitSend entity = transitSendService.getById(transitSendVO.getUid());
        if (entity == null) {
            throw new RuntimeException("该消息不存在");
        }
        if (entity.getStatus() == 2) {
            throw new RuntimeException("该消息已推送");
        }
        if (entity.getStatus() == 3) {
            throw new RuntimeException("该消息已接收");
        }
        //发送微信消息-出入校门提醒
        Transit transit = transitService.getById(entity.getTransitUid());
        transitSendVO.setType(entity.getType());
        transitSendVO.setReasonType(transit.getReasonType());
        transitSendVO.setSourceTime(transit.getSourceTime());
        MessageModelVO messageModelVO = new MessageModelVO();
        //向教师推送
        if (entity.getType() == ETransit.PUSH_TEACHER) {
            GradeTeacherR gradeTeacherR = gradeTeacherRService.getGradeTeacherByEmployeeUid(transit.getEmployeeUid(), 0);
            messageModelVO.setToUid(gradeTeacherR.getEmployeeUid());
            sendAccessSchoolMessage(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_BUSINESS_COMPLETED_REMIND, transitSendVO);
        }
        //向家长推送
        if (entity.getType() == ETransit.PUSH_PARENTS) {
            List<UserEmployeeRVO> userEmployeerList = userEmployeeRService.getByEmployeeUid(transit.getEmployeeUid(), EUser.USER_EMPLOYEE_PARENTS);
            for (UserEmployeeRVO userEmployeeRVO : userEmployeerList) {
                messageModelVO.setToUid(userEmployeeRVO.getUserUid());
                sendAccessSchoolMessage(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_BUSINESS_COMPLETED_REMIND, transitSendVO);
            }
        }
        entity.setStatus(2);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 发送出入校门禁提醒
     *
     * @param messageModelVO
     * @param transitSendVO
     * @return
     */
    public String sendAccessSchoolMessage(MessageModelVO messageModelVO, int platformType, MessageModelType messageModelType, TransitSendVO transitSendVO) {
        messageModelVO.setCompanyUid(transitSendVO.getCompanyUid());
        messageModelVO.setPlatformType(platformType);
        messageModelVO.setType(messageModelType.getType());
        //通行时间
        String sourceTime = DateUtils.formatDateAMPMS(transitSendVO.getSourceTime(), "yyyy年MM月dd日 HH:mm");
        List<SysDictData> reasonType = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("reason_type").get("list")), SysDictData.class);
        for (SysDictData sysDictData : reasonType) {
            if (transitSendVO.getReasonType() == Integer.parseInt(sysDictData.getDictValue())) {
                transitSendVO.setReasonTypeName(sysDictData.getDictLabel());
            }
        }
        String str = "出入通知,【" + transitSendVO.getEmployeeName() + "】" + TransitConstants.getTypeName(transitSendVO.getType()) + "," + sourceTime + ",【原因】" + transitSendVO.getReasonTypeName();
        String content = MessageModelType.getParams(platformType, messageModelType, str);
        messageModelVO.setContent(content);
        return messageModelService.addSendMessage(messageModelVO);
    }
}
